package cheme.ProcessEditor.Figures;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.ScrollBar;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.RowLayout;

import cheme.ProcessEditor.ChemeConstants;
import cheme.ProcessEditor.ChemePresentation;
import cheme.ProcessEditor.Figures.Util.TitleFig;
import cheme.ProcessEditor.Parts.AnnotationPart;
import cheme.ProcessEditor.Table.ListFig;
import cheme.ProcessModel.Annotation;
import cheme.ProcessModel.ConsoleModel;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.IPropertyAware;
import cheme.ProcessModel.ProcessModelConstants;
import cheme.ProcessModel.Annotations.IAnnotation;


public class ConsoleViewFig extends ListFig
{
	//private ScrollPane scrpane;
	//private Label lbl;
	private TextFlow tf;	
	private static FlowPage oldfp;
	private static HashMap<String,TextFlow> msgMap = new HashMap<String,TextFlow>(); // key,value
	public static final Color myYellow     = new Color(null, 255, 255, 153);
//	protected static final Image expandImage = ChemeConstants.DESC_EXPAND.createImage();
	
	public ConsoleViewFig()
	{
		super();
		/*scrpane = new ScrollPane();
		scrpane.getVerticalScrollBar();
		scrpane.setVerticalScrollBarVisibility(2);
		Viewport vp = scrpane.getViewport();
		scrpane.setViewport(vp);*/
		//scrpane.setContents(this);
		
		TitleFig title = new TitleFig("Feedback Messages - This feature is not supported in this version of the software.");
		title.setToolTip(new Label("This presents current feedback messages!"));
	
		ToolbarLayout layout = new ToolbarLayout(false);
		layout = new ToolbarLayout();
		layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
		layout.setStretchMinorAxis(true);
		layout.setObserveVisibility(true);
		layout.setSpacing(insideSpacing);		
		this.setLayoutManager(layout);
		

		
		add(title);	
		
	
		//add(scrpane);
	}
	
	//  Create a new lable with the given text
	// and add it to the list fig
	public void setMessage(String newMessage) 
	{		
		//lbl = new Label();		
		//lbl.setText(newMessage);
		
		// Added Text flow to have word wraapping in the console View
		tf = new TextFlow(newMessage);
		
		// Pa commented the mouse listeners for non feedback version
	   /* tf.addMouseListener(new MouseListener()
		{
			public void mousePressed(MouseEvent me)
			{
				handleMouseClickEvent(me);				
			}

			public void mouseDoubleClicked(MouseEvent me) 
			{
				
			}
			public void mouseReleased(MouseEvent me) {}
		});*/
	    
	    // PA added this for non feedback version
	    tf.setText("This feature is not supported in this version of the software.");
	    FlowPage fp = new FlowPage();
	    fp.add(tf);
	    this.add(fp);
	    
	    // Adding the entry in the map
	    String msgIndex = newMessage.substring(0, 2);
	    msgIndex.trim();
	    msgMap.put(msgIndex, tf);
	   
	}
	
	
	protected void handleMouseClickEvent(MouseEvent me) 
	{
		TextFlow textObj = (TextFlow)me.getSource();
		String text = textObj.getText();
		IModelElement key = null;
		HashMap<IModelElement,String>msgMap = ConsoleModel.getMap();
		if(null!= msgMap)
		{
			Iterator It = msgMap.keySet().iterator();
	        while (It.hasNext())
	        {
	        	IModelElement elementkey = (IModelElement)(It.next());        	
	            //System.out.println(consoleMap.get(elementkey));
	        	String message = msgMap.get(elementkey);
	        	if(message.equals(text))
	        	{
	        		key = elementkey;
	        		
	        		break;
	        	}
	         }
	        if(key != null)
	        {
	        	// get the flow page for current text flow on which mouse is clicked
	        	FlowPage currentfp = (FlowPage) textObj.getParent();	      
	        	boolean bvisible = true;
	        	// Do nothing
	        	if(null == currentfp){
	        		return;
	        		}
	        	
	        	// set the b/g color of the cuurent message to yellow
	        	
	        	currentfp.setBackgroundColor(myYellow);
        		currentfp.setOpaque(true);
        		String elementString = key.toString().trim()+ ", ";
        		
	        	// if oldfp is null, i.e first click
	        	if(oldfp == null)
	        	{
	        		oldfp = currentfp;
	        		//	 Clicked on a new annotation. log this as message read	        		
	        		ProcessModelConstants.log.Write("Read, ",elementString, text);
	        		
	        	}
	        	// if click is done on new messge, change the b/g color of old flow page to white
	        	else if(oldfp != currentfp)
	        	{
	        		oldfp.setBackgroundColor(ColorConstants.white);
	        		oldfp.setOpaque(true);	        		
	        		// update the oldfp with current fp
	        		oldfp = currentfp;
	        		
	        		// Clicked on a new annotation. log this as message read	        		
	        		ProcessModelConstants.log.Write("Read, ",elementString, text);
	        		
	        	}
	        	// means currentfp == oldfp, clicked twice on the same message
	        	else
	        	{
	        		//reset the b/g color to white
	        		currentfp.setBackgroundColor(ColorConstants.white);
	        		currentfp.setOpaque(true);
	        		// update the old fp. Now nothig is highlighted
	        		oldfp = null;
	        		bvisible = false; // hide the tooltip
	        	}
	        	
	        	
        		/***** manipulation of tool tip begins *****/
        		
	        	// get the annotation figure
	        	AnnotationFig fig = AnnotationPart.getAnnotationFig(key);
	        	       	
	        	if (null != fig)
	        	{						        	
		        	// get the location of annotation fig
		        	Point location = new Point(fig.getLocation());
		        	// call the handlemouseevent() of annotation part to show the tooltip
		        	String msgtoShow = (text.substring(2));
		        	msgtoShow.trim();
		        	AnnotationPart.togglebgColor(fig);
		        	AnnotationPart.handlemouseClick(location, msgtoShow,bvisible);
	        	}
	        	/***** manipulation of tool tip ends *****/
	        	
	        }
	       }
		
	}
	
	// This function is used to togglet the conview state
	// this is called from annotation part. onmouseclick
	public static void setConsoleMsgbgColor(String message)
	{
		TextFlow textObj =  msgMap.get(message);
		
		if(textObj!= null)
		{
			FlowPage currentfp = (FlowPage) textObj.getParent();	
			if(null == currentfp)
			{
        		return;
        	}
			// set the b/g color of the cuurent message to yellow
			currentfp.setBackgroundColor(myYellow);
    		currentfp.setOpaque(true);
    		
        	// if oldfp is null, i.e first click
        	if(oldfp == null)
        	{
        		oldfp = currentfp;
        		
        	}
        	// if click is done on new messge, change the b/g color of old flow page to white
        	else if(oldfp != currentfp)
        	{
        		oldfp.setBackgroundColor(ColorConstants.white);
        		oldfp.setOpaque(true);	        		
        		// update the oldfp with current fp
        		oldfp = currentfp;
        		
        	}
        	// means currentfp == oldfp, clicked twice on the same message
        	else
        	{
        		//reset the b/g color to white
        		currentfp.setBackgroundColor(ColorConstants.white);
        		currentfp.setOpaque(true);
        		// update the old fp. Now nothig is highlighted
        		oldfp = null;        		
        	}
		}
	}
	
	public void clearAll() 
	{		
		msgMap.clear();
		this.removeAll();
		TitleFig title = new TitleFig("Feedback Messages");
		title.setToolTip(new Label("This presents current feedback messages!"));
		add(title);	
		
	}
		
	
	protected void initPresentation()
	{
		super.initPresentation();

		setPresentation(
				ChemePresentation.TableEditors.Equations.background,
				ChemePresentation.TableEditors.Equations.foreground,
				ChemePresentation.TableEditors.Equations.border
		);
	}

	

}
